package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.entity.domain.customer.CustomerLabel;
import com.probox.common.entity.domain.customer.CustomerLabelInfo;
import com.probox.common.entity.mapper.customer.CustomerLabelInfoMapper;
import com.probox.common.entity.mapper.customer.CustomerLabelMapper;
import com.probox.common.entity.service.customer.ICustomerLabelInfoService;
import com.probox.common.entity.service.customer.ICustomerLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class CustomerLabelInfoServiceImpl extends ServiceImpl<CustomerLabelInfoMapper, CustomerLabelInfo> implements ICustomerLabelInfoService {

    @Autowired
    private CustomerLabelInfoMapper customerLabelInfoMapper;

    /**
     * 根据客户查询对应的标签
     *
     * @param customerId
     * @return
     */
    @Override
    public List<CustomerLabelInfo> getByCustomerId(Long customerId) {
        return customerLabelInfoMapper.getByCustomerId(customerId);
    }

    /**
     * 删除标签
     *
     * @param infoId
     * @return
     */
    @Override
    public boolean delLabel(Long infoId) {
        UpdateWrapper<CustomerLabelInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("info_id", infoId);
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    /***
     * 添加和修改客户标签
     * @param customerId
     * @param list
     * @return
     */
    @Override
    public boolean addOrEdit(Long customerId, List<CustomerLabelInfo> list) {
        UpdateWrapper<CustomerLabelInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("customer_id", customerId);
        updateWrapper.ne("DATA_STATUS",DataStatus.delete.getKey());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        this.update(updateWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            list.stream().forEach(vo -> {
                vo.setCustomerId(customerId);
                vo.setDataStatus(DataStatus.valid.getKey());
                if (ObjectUtil.isNotEmpty(vo.getInfoId())) {
                    vo.setUpdateName(SecurityUtils.getUsername());
                    vo.setUpdateTime(DateUtil.date());
                    vo.setUpdateUid(SecurityUtils.getUserId());
                    this.updateById(vo);
                } else {
                    vo.setCreateName(SecurityUtils.getUsername());
                    vo.setCreateTime(DateUtil.date());
                    vo.setCreateUid(SecurityUtils.getUserId());
                    this.save(vo);
                }
            });
        }
        return true;
    }


}
